import React, { Component } from 'react';
import { Form, Button, Input, Toast } from 'antd-mobile'
import { login } from '../../api'
class Index extends Component {
	submit = async value => {
		const resp = await login(value)
		const { code, token, mess } = resp.data
		if (code === 200) {
			Toast.show({
				content: mess,
				duration: 1000,
			})
			localStorage.setItem('token', token)
			this.props.history.push({ pathname: '/' })

		} else {
			Toast.show({
				content: mess,
				duration: 1000
			})
		}
	}
	render() {
		return (
			<div>
				<Form
					onFinish={this.submit}
					layout='horizontal'
					footer={
						<Button block type='submit' color='primary'>
							登录
						</Button>
					}
				>
					<Form.Item
						name='username'
						label='用户名'
						rules={[{ required: true, message: '用户名不能为空' }]}
					>
						<Input placeholder='请输入用户名' />
					</Form.Item>
					<Form.Item name='password' label='密码'
						rules={[{ required: true, message: '密码不能为空' }]}
					>
						<Input placeholder='请输入密码' type="password" />
					</Form.Item>
				</Form>
			</div>
		);
	}
}

export default Index;
